from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.spuSerializer import SPUSerializer, BrandSerializer, GoodCategorySerializer
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.utils import PaginatorShow
from meiduo_admin.permissions import *


class SPUView(ModelViewSet):
    queryset = SPU.objects.all().order_by("id")
    serializer_class = SPUSerializer
    permission_classes = [IsAdminUser]

    pagination_class = PaginatorShow


class BrandView(ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandSerializer
    permission_classes = [IsAdminUser]


class GoodCategoryView(ModelViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = GoodCategorySerializer
    permission_classes = [IsAdminUser]

    def get_queryset(self):
        pk = self.kwargs.get("pk")
        if pk:
            return self.queryset.filter(parent_id=pk)
        return self.queryset.filter(parent_id__isnull=True)


